<template>
	<view class="container">
		<!-- 头部 -->
		<view class="title">
			<view class="bgcode" @click="search_btn = false">
				<image src="../../static/index/bgcode.png" mode="scaleToFill"></image>
			</view>
			<view class="searchInput">
				<input type="text" v-model="searchObj.text" @focus="search_btn = true" placeholder="请输入要搜索的内容" />
			</view>
			<view class="user_head">
				<image :src="userInfos.headImg || '../../static/index/notSign.png'" mode="scaleToFill" v-if="!search_btn"></image>
				<button type="default" v-else @click="search_product()">搜索</button>
			</view>
		</view>
		<!-- 轮播图 -->
		<view class="banner" @click="search_btn = false">
			<swiper class="swiper" :indicator-dots="bannerList.length > 1" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
				<swiper-item class="swiper-item" v-for="(item,index) in bannerList">
					<view class="swiper-item-view">
						<image class="swpier-image" :src="item.bannerUrl" mode="scaleToFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 半圆 -->
		<view class="borderRadius" @click="search_btn = false"></view>
		<!-- 分类 -->
		<view class="fication" v-if="ficationList.length > 0" @click="search_btn = false">
			<swiper class="swiper" :indicator-dots="true" indicator-active-color="#ffa02c" :autoplay="false" :circular="true">
				<swiper-item class="swiper-item" v-if="ficationList.length > 0">
					<view class="swiper-item-view" v-for="(item,index) in ficationList"  v-if="index >= 0 && index < 10" @click="navTo('../classification/index?_id='+item._id)">
						<image class="swpier-image" :src="item.classification_name" mode="scaleToFill"></image>
						<view class="classification_name">
							{{item.classification_url}}
						</view>
					</view>
					<view style="clear: both;">	</view>
				</swiper-item>
				<swiper-item class="swiper-item" v-if="ficationList.length > 10">
					<view class="swiper-item-view" v-for="(item,index) in ficationList" v-if="index >= 10 && index < 20" @click="navTo('../classification/index?_id='+item._id)">
						<image class="swpier-image" :src="item.classification_name" mode="scaleToFill"></image>
						<view class="classification_name">
							{{item.classification_url}}
						</view>
					</view>
					<view style="clear: both;">	</view>
				</swiper-item>
				<swiper-item class="swiper-item" v-if="ficationList.length > 20">
					<view class="swiper-item-view" v-for="(item,index) in ficationList" v-if="index >= 20 && index < 30" @click="navTo('../classification/index?_id='+item._id)">
						<image class="swpier-image" :src="item.classification_name" mode="scaleToFill"></image>
						<view class="classification_name">
							{{item.classification_url}}
						</view>
					</view>
					<view style="clear: both;">	</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 活动窗口 -->
		<view class="activity" v-if="activityList.length > 0" @click="search_btn = false">
			<view class="activity_desc" v-for="(item,index) in activityList" @click="navTo('activity/index?id='+ item._id)">
				<view class="activity_image">
					<image :src="item.acvicity_image" mode="scaleToFill"></image>
				</view>
				<view class="activity_title">
					{{item.acvicity_desc,setTime(index,item.acvicity_open_time)}}
				</view>
			</view>
			<view style="clear: both;"></view>
		</view>
		<!-- 首页商品展示 -->
		<view class="product" @click="search_btn = false">
			<view class="productLeft">
				<view v-if="index % 2 != 0" class="content" v-for="(item, index) in productList" :key="index" @click="navTo('../product/index?_id='+item._id)">
					<image :src="item.produce_info.product_important_img" mode="widthFix"></image>
					<view class="itemTitle">{{ item.produce_info.name }}</view>
					<view class="itemPrice">
						<view class="left">￥{{ item.produce_info.product_price }}</view>
						<view class="right">已售{{ item.produce_info.product_sale }}</view>
					</view>
				</view>
			</view>
			<view class="productRight">
				<view v-if="index % 2 == 0" class="content" v-for="(item, index) in productList" :key="index" @click="navTo('../product/index?_id='+item._id)">
					<image :src="item.produce_info.product_important_img" mode="widthFix"></image>
					<view class="itemTitle">{{ item.produce_info.name }}</view>
					<view class="itemPrice">
						<view class="left">￥{{ item.produce_info.product_price }}</view>
						<view class="right">已售{{ item.produce_info.product_sale }}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="clear" v-if="productLoadBottom">加载中...</view>
		<!-- #ifdef APP-PLUS -->
		<appUpdate ></appUpdate>
		<!-- #endif -->
	</view>
</template>

<script>
	import api from '@/comon/api.js'
	// #ifdef APP-PLUS
	import appUpdate from '@/components/app_update/index.vue'
	// #endif
	export default {
		components:{
			// #ifdef APP-PLUS
			appUpdate
			// #endif
		},
		data() {
			return {
				userInfos:{
					headImg:''
				},
				bannerList:[],
				ficationList:[],
				activityList:[],
				productPage: {
					pageNum : 0,
					pageSize: 10,
					product_type: 0
				},
				productList: [],
				productLoad: false,
				productLoadBottom: false,
				searchObj:{
					text:""
				},
				search_btn: false
			}
		},
		 async onLoad() {
			this.loadBanner();
			this.classification();
			this.activity();
			this.product();
		},
		onShow() {
			this.loadData();
		},
		onReachBottom() {
			this.productLoadBottom = true;
			if(!this.productLoad){
				return
			}
			this.productPage.pageNum+=1;
			this.product();
		},
		methods: {
			async loadData(){
				let userinfo = uni.getStorageSync('userInfos');
				if(userinfo){
					this.userInfos = userinfo
				}
			},
			// 获取轮播图
			async loadBanner(){
				this.bannerList = uni.getStorageSync('bannerList');
				let res = await api.uniCloudApi(api.publicFun.banner,{type:0 , banner_type: 0});
				if(res.length > 0){
					this.bannerList = res;
					uni.setStorageSync('bannerList',res)
				}
			},
			// 获取分类
			async classification(){
				this.ficationList = uni.getStorageSync('ficationList');
				let res = await api.uniCloudApi(api.publicFun.classification,{class_type: 0});
				if(res.length > 0){
					this.ficationList = res
					uni.setStorageSync('ficationList',res)
				}
			},
			// 获取活动
			async activity(){
				this.activityList = uni.getStorageSync('activityList');
				let res = await api.uniCloudApi(api.publicFun.activity,{activity_type: 0});
				if(res.length > 0){
					this.activityList = res;
					uni.setStorageSync('activityList',res)
				}
			},
			//获取首页商品
			async product(){
				this.productList = uni.getStorageSync('productList');
				let res = await api.uniCloudApi(api.publicFun.product,this.productPage);
				if(res.length > 0){
					if(this.productPage.pageNum == 0){
						this.productList = [];
						this.productLoad = true
						uni.setStorageSync('productList',res);
					}
					this.productList = this.productList.concat(res);
				}
				if(res){
					let that = this;
					setTimeout(() => {
						that.productLoadBottom = false
					},1500)
				}
			},
			//商品模糊搜索
			async search_product(){
				if(this.searchObj.text == ''){
					uni.showToast({
						icon:'none',
						title:'请输入搜索内容'
					})
					return 
				}
				let search_text = this.searchObj.text;
				this.searchObj.text = '';
				this.search_btn = false;
				this.navTo('./search/index?text=' + search_text);
			},
			//活动时间选择
			setTime(index,time){
				let that = this;
				let nowTimeStap = (new Date()).getTime();
				// 当前时间是否大于活动开始时间
				if(time - nowTimeStap > 0){
					const invite = setInterval(() => {
						// 当前时间大于一天
						nowTimeStap = (new Date()).getTime();
						if(time - nowTimeStap > 86400000){
							let times = time - nowTimeStap;
							// 总秒数
							var second = Math.floor(times / 1000);
							// 天数
							var day = Math.floor(second / 3600 / 24);
							// 小时
							var hr = Math.floor(second / 3600 % 24);
							// 分钟
							var min = Math.floor(second / 60 % 60);
							that.activityList[index].acvicity_desc = "倒计时:" +  day + "天" + hr + "时" + min + "分";
							if(time - nowTimeStap < 0){
								clearInterval(invite)
							}
						}else{
							let times = time - nowTimeStap;
							// 总秒数
							var second = Math.floor(times / 1000);
							// 小时
							var hr = Math.floor(second / 3600 % 24);
							// 分钟
							var min = Math.floor(second / 60 % 60);
							// 秒
							var sec = Math.floor(second % 60);
							that.activityList[index].acvicity_desc = "倒计时:" + hr + "时" + min + "分" + sec + "秒";
							if(time - nowTimeStap < 0){
								clearInterval(invite)
							}
						}
					},1000)
				}else {
					that.activityList[index].acvicity_desc = '进入主会场'
				}
			},
			navTo(url){
				uni.navigateTo({
					url:url
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #ff46af;
	}
	.container{
		width: 100%;
	}
	// 顶部搜索框，扫一扫，用户头像
	.title{
		width: 96%;
		padding: 0 2%;
		background-color: $uni-back-color;
		height: 55px;
		display: flex;
		position: fixed;
		top: 0;
		z-index: 1;
		/* #ifdef APP-PLUS */
		padding-top: 44px;
		/* #endif */
		.bgcode{
			width: 10%;
			text-align: center;
			margin-top: 5px;
			image{
				margin-top: 7px;
				width: 30px;
				height: 30px;
			}
		}
		.searchInput{
			width: 80%;
			margin-top: 5px;
			input{
				width: 90%;
				padding: 0 5%;
				height: 30px;
				line-height: 30px;
				background-color: #FFFFFF;
				border-radius: 15px;
				margin-top: 7px;
			}
		}
		.user_head{
			width: 10%;
			text-align: center;
			margin-top: 5px;
			image{
				margin-top: 7px;
				width: 30px;
				height: 30px;
				border-radius: 15px;
			}
			button{
				background-color: rgba(0,0,0,0);
				color: #FFFFFF;
				font-size: 20rpx;
				width: 100%;
				height: 30px;
				line-height: 30px;
				padding: 0;
				margin-top: 7px;
				border: 0;
				&::after{
					border: 0;
				}
			}
		}
	}
	//首页轮播图
	.banner{
		width: 100%;
		background-color: $uni-back-color;
		/* #ifdef H5 */
		margin-top: 55px;
		/* #endif */
		/* #ifdef APP-PLUS */
		margin-top: 99px;
		/* #endif */
		.swiper{
			width: 100%;
			height: 365rpx;
			.swiper-item{
				width: 100%;
				height: 100%;
				.swiper-item-view{
					width: 100%;
					height: 100%;
					.swpier-image{
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}
	// 半圆
	.borderRadius{
		width: 100%;
		height: 20rpx;
		background-color: $uni-back-color;
		border-radius: 100% 100% 0 0;
		position: relative;
		top: -10rpx;
	}
	// 分类
	.fication{
		width: 100%;
		background-image: linear-gradient(to bottom, $uni-back-color,#ff7142);
		padding: 1% 0 3% 0;
		position: relative;
		top: -10rpx;
		.swiper{
			width: 94%;
			height: 420rpx;
			border-radius: 20rpx;
			background-color: #FFFFFF;
			margin-left: 3%;
			.swiper-item{
				width: 100%;
				height: 100%;
				.swiper-item-view{
					width: 20%;
					height: 175rpx;
					text-align: center;
					float: left;
					image{
						margin-top: 20rpx;
						width: 110rpx;
						height: 110rpx;
					}
				}
			}
		}
	}
	// 活动
	.activity{
		width: 100%;
		position: relative;
		top: -11rpx;
		background-image: linear-gradient(to bottom, #ff7142, #ff9a6b);
		.activity_desc{
			width: 45%;
			height: 210rpx;
			margin: 10rpx 2.5%;
			float: left;
			position: relative;
			border-radius: 15rpx;
			.activity_image{
				width: 100%;
				height: 100%;
				image{
					width: 100%;
					height: 100%;
					border-radius: 15rpx;
				}
			}
			.activity_title{
				position: absolute;
				bottom: 0;
				width: 100%;
				text-align: center;
				background-color: rgba(255, 64, 64, 0.3);
				color: #2e93ea;
				font-size: 26rpx;
				height: 50rpx;
				line-height: 50rpx;
			}
		}
	}
	//商品
	.product{
		width: 100%;
		display: flex;
		position: relative;
		background-image: linear-gradient(to bottom, #ff9a6b, #ff46af);
		top: -13rpx;
		.productLeft,
		.productRight {
			width: 50%;
			.content {
				width: 90%;
				margin: 0 5%;
				box-shadow: 0 0 15rpx 0 #dcdcdc;
				border-radius: 25rpx;
				margin-top: 15rpx;
				margin-bottom: 25rpx;
				float: left;
				background-color: #FFFFFF;
				image {
					border-radius: 25rpx 25rpx 0 0;
					width: 100%;
				}
				.itemTitle {
					width: 95%;
					padding: 0 2.5%;
					line-height: 35rpx;
				}
				.itemPrice {
					width: 90%;
					padding: 0 5%;
					display: flex;
					height: 55rpx;
					view {
						width: 50%;
						height: 55rpx;
						line-height: 55rpx;
					}
					.left {
						color: #e54d42;
						font-weight: bold;
						font-size: 26rpx;
					}
					.right {
						color: #999999;
						text-align: right;
						font-size: 22rpx;
					}
				}
			}
		}
	}
</style>
